package com.cole.miniapp.project.controller;

import com.cole.miniapp.common.annotation.LoginUser;
import com.cole.miniapp.common.bean.ResponseResult;
import com.cole.miniapp.project.domain.FcmUser;
import com.cole.miniapp.project.domain.FcmUserRecharges;
import com.cole.miniapp.project.service.PayService;
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@RestController
@RequestMapping("/api/pay")
public class WxPayController {


	@Autowired
	private PayService payService;

	/**
	 * 创建用户充值订单
	 * @param fcmUser
	 * @param money
	 * @return
	 */
	@PostMapping(value = "/createRecharge")
	public ResponseResult createRecharge(@LoginUser FcmUser fcmUser,
										 @RequestParam(value = "total") BigDecimal money){
		ResponseResult result = ResponseResult.success();

		int i=money.compareTo(BigDecimal.ZERO);
		if (i==1){
			FcmUserRecharges fcmUserRecharges=payService.createRecharge(fcmUser,money);
			result.setData(fcmUserRecharges);
			return result;
		}
		result=ResponseResult.error();
		result.setMsg("充值金额不能小于0！");
		return result;
	}


	/**
	 * 支付充值订单
	 * @param fcmUser
	 * @param orderNumber
	 * @return
	 * @throws WxPayException
	 */
	@PostMapping(value = "/payRecharge")
	public ResponseResult payRecharge(@LoginUser FcmUser fcmUser,
									  @RequestParam(value = "orderNumber") String orderNumber) throws WxPayException {

		WxPayMpOrderResult reponse=payService.payRecharge(fcmUser,orderNumber);
		ResponseResult result = ResponseResult.success();
		result.setData(reponse);
		return result;
	}

}












